package com.ybfenpan.controller.admin.member;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.model.Member;
import com.ybfenpan.model.log.LogCenterUp;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.log.LogCenterUpService;
import com.ybfenpan.utils.CryptUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/3/25.
 */
@Controller
 
public class MemberUpdateController extends BaseController {
	@Resource
    private MemberService memberService;

	@Resource
	private LogCenterUpService logCenterUpService;
   
   

    @RequestMapping("admin/member/memberUpdateShow.shtml")
    public String show(HttpServletRequest request) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Member member = memberService.getByKey(id);
        	String onePassword = CryptUtil.getInstance().decrypt2String(member.getOnePassword(),CryptUtil.keyStr);
        	String twoPassword = CryptUtil.getInstance().decrypt2String(member.getTwoPassword(),CryptUtil.keyStr);
        	
        	request.setAttribute("onePassword",onePassword);
        	request.setAttribute("twoPassword",twoPassword);
        	request.setAttribute("member",member);
        }
        return "admin/member/memberUpdate";
       
    }
    
    
    @RequestMapping("admin/member/memberUpdate.shtml")
    public String update(Member member , HttpServletRequest request, ModelMap model) throws Exception{
        Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
       
        if(0 < id){
        	 
        	String onePsw= MapUtil.getString(formMap,"onePassword");
    		String onePassword = CryptUtil.getInstance().encrypt2String(onePsw,CryptUtil.keyStr);
    		
    		String twoPsw= MapUtil.getString(formMap,"twoPassword");
    		String twoPassword = CryptUtil.getInstance().encrypt2String(twoPsw,CryptUtil.keyStr);

    		Member member1=this.memberService.getByKey(id);
    		Integer oldLevel=member1.getHistoryBuyStock();
    		Integer newLevel=MapUtil.getInteger(formMap,"level");
    		
    		 
    		Map mapMember = new HashMap();
    		mapMember.put("id",id);
    		mapMember.put("onePassword",onePassword);
    		mapMember.put("twoPassword",twoPassword);
    		mapMember.put("pwdAnswer",MapUtil.getString(formMap,"pwdAnswer"));
    		mapMember.put("email",MapUtil.getString(formMap,"email"));
    		mapMember.put("tel",MapUtil.getString(formMap,"tel"));
    		mapMember.put("qq",MapUtil.getString(formMap,"qq"));
    		mapMember.put("wei",MapUtil.getString(formMap,"wei"));
    		mapMember.put("personName",MapUtil.getString(formMap,"personName"));
    		mapMember.put("cardNo",MapUtil.getString(formMap,"cardNo"));
        	mapMember.put("remark",MapUtil.getString(formMap,"remark"));
    		mapMember.put("bankName",MapUtil.getString(formMap,"bankName"));
    		mapMember.put("bankPersonName",MapUtil.getString(formMap,"bankPersonName"));
    		mapMember.put("bankAddress",MapUtil.getString(formMap,"bankAddress"));
    		mapMember.put("bankCardNo",MapUtil.getString(formMap,"bankCardNo"));
    		mapMember.put("historyBuyStock",newLevel);

    		mapMember.put("bankSheng",MapUtil.getString(formMap,"bankSheng"));
    		mapMember.put("bankShi",MapUtil.getString(formMap,"bankShi"));
            
    		mapMember.put("zhifuName",MapUtil.getString(formMap,"zhifuName"));
    		mapMember.put("zhifuCardNo",MapUtil.getString(formMap,"zhifuCardNo"));
    		 
    		mapMember.put("xiaNum",MapUtil.getDoubleValue(formMap,"xiaNum"));

			if (!"".equals(newLevel)){
				LogCenterUp logCenterUp=new LogCenterUp();
				logCenterUp.setMemberId(member1.getId());
				logCenterUp.setOldDailiId(oldLevel);
				logCenterUp.setNewDailiId(newLevel);
				logCenterUp.setTitle("管理员修改了你的服务中心等级");
				logCenterUp.setCommitDate(new Date());
				logCenterUpService.saveOrUpdate(logCenterUp);
				mapMember.put("accountCenter","T");
				mapMember.put("historyBuyStock",newLevel);
			}

        	memberService.update(mapMember);
            setMessage(model,"修改成功！");


            
            Boolean result = true;
    		request.setAttribute("result", result);
            
        	request.setAttribute("onePassword",onePsw);
        	request.setAttribute("twoPassword",twoPsw);
            request.setAttribute("member",member);
            
        }
        return "admin/member/memberUpdate";
    }

     
    

}
